/**
 * Created by Administrator on 2016/5/8.
 */
// 创建express实例
const express = require('express');
const app = new express();

// 创建Server和Socket
const server = require('http').createServer(app);
const socket = require('socket.io')(server);

// socket监听连接事件
socket.on('connection', function (client) {
    console.info('A client has connected...');

    // 接受用户的nickname
    client.on('join', function (nickname) {
        client.nickname = nickname;
    });

    // socket监听消息事件
    client.on('message', function (data) {
        // 打印到控制台
        console.info(data);
        // 对其他客户端进行广播
        client.broadcast.emit('message',client.nickname+" : "+data);
        // 对当前客户端发送消息
        client.emit('message', client.nickname+" : "+data);
    });
});
// 使express接收到请求后跳转页面
app.get('/', function (req, res) {
   res.sendFile(__dirname+'/html/chatroom.html');
});


// 使server监听8080端口
server.listen(8080);
